import { configModel,categoryModel,userModel } from './apis/index.js';
import util from './utils/index.js';
import defaultConfigInfo from './config.js';
App({
  onLaunch: function () {
    //获取配置
    let that = this;
    //that.getConfigInfo()
    //that.getUserInfo()
    //that.getCategoryInfo()
    //that.getLocationInfo()
  },
  globalData: {
    configInfo:false,
    userInfo: false,
    categoryInfo:false,
    locationInfo:false,
    showAlertAd:true
  },
  //获取全局配置信息
  getConfigInfo: function (refrensh = false) {
    let that = this;
    let configInfo = that.globalData.configInfo;
    return new Promise((resove, reject) => {
      if (configInfo && !refrensh) {
        let allConfigInfo = Object.assign({}, configInfo,defaultConfigInfo)
        resove(allConfigInfo)
      } else {
        configModel.getConfig({ name: "site_config"}).then((d) => {
          let configInfo = d.data
          
          //赋值全局配置变量
          that.globalData.configInfo = configInfo;
          let allConfigInfo = Object.assign({}, configInfo,defaultConfigInfo)
          console.log(allConfigInfo)
          resove(allConfigInfo)
        })
      }
    }).then((allConfigInfo) => {
      //设置全局tabbar
      //util.setTabbar(configInfo.tabbar)
      return allConfigInfo
    })
  },
  //获取全局用户信息
  getUserInfo: function (refrensh = false) {
    let that = this;
    let userInfo = that.globalData.userInfo;
    return new Promise((resove, reject) => {
      //如果已经获取了,并且不刷新
      if (userInfo && !refrensh) {
        resove(userInfo)
      } else {
        userModel.getAllInfo().then((d) => {
          let userInfo=d.data;
          that.globalData.userInfo = userInfo;
          // 有私信红点提示
          let chat = userInfo.number.chat;
          if (chat){
            wx.showTabBarRedDot({
              index: 4
            })
          }else{
            wx.hideTabBarRedDot({
              index: 4,
            })
          }
          
          resove(userInfo)
        })
      }
    })
  },
  //获取全局分类信息
  getCategoryInfo: function (refrensh = false) {
    let that = this;
    let categoryInfo = that.globalData.categoryInfo;
    return new Promise((resove, reject) => {
      //如果已经获取了,并且不刷新
      if (categoryInfo && !refrensh) {
        resove(categoryInfo)
      } else {
        categoryModel.getAll().then((d) => {
          that.globalData.categoryInfo = d.data;
          resove(d.data)
        })
      }
    })
  },
  //获取全局定位信息
  getLocationInfo: function (refrensh = false) {
    let that = this;
    let locationInfo = that.globalData.locationInfo;
    return new Promise((resove, reject) => {
      //如果已经获取了,并且不刷新
      if (locationInfo && !refrensh) {
        resove(locationInfo)
      } else {
        util.getLocation().then((d)=>{
          let AllLocationInfo=d;
          console.log(d)
          let { latitude, longitude,name } = AllLocationInfo;
          let city = AllLocationInfo.regeocodeData.addressComponent.city;
          let locationInfo={
            latitude,
            longitude,
            city,
            address: name,
          }
          let location=wx.getStorageSync('location');
          if (!location){
            wx.setStorageSync('location', locationInfo)
          }
          that.globalData.locationInfo = locationInfo;
          resove(locationInfo)
        })
      }
    })
  }
})